<?php
namespace Hive\Ressource;
use Hive\Company;
use Hive\Ressource;
/**
 * Experience Object.
 *
 * An experience is a job position, a ressource can have multiple experience,
 * in the same or different company, each time that the position change or the company,
 * it generate a new experience
 *
 * @author Xavier VILAIN
 * @package ressource
 * @property int	$ressource_id	Ressource Id
 * @property int	$expid			Experience Id
 * @property date	$start_date		Start Experience Date (can be null)
 * @property date	$end_date		End Experience Date (can be null)
 * @property int	$compid			Company Id
 * @property string	$jobtitle		Job Title
 * @property string	$contract_type	Contract Type
 * @property int	$salary			Salary
 * @property int	$bonus			Bonus
 * @property bool	$mobility		Does the contact be interested to work
 * international or faraway from is home
 * @property string	$comment		Comment on the job
 * @property datetime 	$udate		Change Date
 * @property datetime 	$uname		Author
 */
class Experience extends \Fox\Object {

	/**
	 * Link To Ressource
	 * @var string
	 */
	const LINK_RESSOURCE = 'ressource';

	/**
	 * Contract Type list
	 * CDD
	 * @var string
	 */
	const CONTRACT_CDD = '1';

	/**
	 * Contract Type list
	 * Apprentice
	 * @var string
	 */
	const CONTRACT_APP = '4';

	/**
	 * Contract Type list
	 * Internship
	 * @var string
	 */
	const CONTRACT_INTERNSHIP = '5';

	/**
	 * Reference in an upload file process
	 * @var int
	 */
	const REFERENCE_UPLOAD = 2;
	
	/**
	 * Name of the company
	 * @var string
	 */
	public $companyName = null;

	/**
	 *
	 * Shortcut on Company Object
	 * @var Company
	 */
	private $company = null;

	/**
	 * @param array $fields
	 */
	protected function afterSetFields(array $fields)
	{
		if(!empty($fields['compid']) && empty($fields['companyName']))
		{
			$this->companyName = $this->company($fields['compid'])->company_name;
		}
	}

	/**
	 * Retrieve all ressource (including Address,Experience)
	 * @return array
	 */
	public function getData()
	{
		$data = parent::getData();
		return array_merge($data,array('companyName' => $this->companyName));
	}

	/**
	 *
	 * Enter description here ...
	 */
	public function content()
	{
		$data = array(
				'jobtitle' => $this->jobtitle,
				'mobility' => $this->mobility,
				'contract_type' => $this->contract_type,
				'salary' => $this->salary,
				'bonus' => $this->bonus,
				'comment' => $this->comment
		);

		if(empty($this->companyName) && isset($data['compid'])) {
			$this->companyName = $this->company($data['compid'])->company_name;
		}
		$data['companyName'] = $this->companyName;
		// Convert Droplist into texte value
		$data['mobility'] = empty($date['mobility']) ? null : \Fox\Ddic\DataElement\Text::label('mobility',$date['mobility']);
		$data['contract_type'] = empty($date['contract_type']) ? null : \Fox\Ddic\DataElement\Text::label('contract_type',$date['contract_type']);
		return implode(',',$data);
	}

	/**
	 * (non-PHPdoc)
	 * @see includes/classes/Fox/Object/Versioned::beforeVersionUpdate()
	 */
	protected function beforeCreate() {
		$this->udate = self::sqlDateTime(time());
		$this->uname = \User::current()->getId();
	}

	/**
	 * (non-PHPdoc)
	 * @see Fox.Object::afterCreate()
	 */
	protected function afterCreate() {
		// Add to the search engine
		$searchEngine = new \Fox\SearchEngine();
		$searchEngine->synchronize($this,$this->ressource_id,Ressource::classname());
	}

	/**
	 * (non-PHPdoc)
	 * @see includes/classes/Fox/Object/Versioned::beforeVersionUpdate()
	 */
	protected function beforeUpdate($changed)
	{
		if($changed) {
			$this->udate = self::sqlDateTime(time());
			$this->uname = \User::current()->getId();
		}
		return $changed;
	}

	/**
	 *
	 * Check if the contract type need to have an end date
	 * @param string $contract_type
	 * @return bool true if need a validation
	 */
	static function needEndDate($contract_type = null)
	{
		if($contract_type == self::CONTRACT_APP ||
			$contract_type == self::CONTRACT_CDD ||
			$contract_type == self::CONTRACT_INTERNSHIP )
		{
			return true;
		}
		return false;
	}

	/**
	 * (non-PHPdoc)
	 * @see includes/classes/Fox/Object/Versioned::afterVersionUpdate()
	 */
	protected function afterUpdate()
	{
		// Add to the search engine
		$searchEngine = new \Fox\SearchEngine();
		$searchEngine->synchronize($this,$this->ressource_id,Ressource::classname());
	}

	/**
	 * (non-PHPdoc)
	 * @see Fox.Object::delete()
	 */
	public function delete() {
		parent::delete();
		$searchEngine = new \Fox\SearchEngine();
		$searchEngine->delete($this,$this->ressource_id,Ressource::classname());
	}
	
	/**
	 * @return float Salary + Bonus
	 */
	public function fullSalary()
	{
		return $this->salary + $this->bonus;
	}

	/**
	 *
	 * Find and return Company Object
	 * @return Company
	 */
	public function company($id = null)
	{
		try {
			$cid = empty($id) ? $this->compid : $id;
			$this->company = \Fox\Dao::load(Company::className(),$cid);
		}
		catch (\Fox\Object\Exception\NotExist $e)
		{
			$this->company = new Company();
		}
		return $this->company;
	}

	/**
	 * Company Name
	 * @return string
	 */
	public function companyName()
	{
		if(empty($this->companyName)) {
			$this->companyName = $this->company()->name();
		}
		return $this->companyName;
	}
	
	/**
	 * 
	 * Label of a Job
	 * @return string
	 */
	public function label() {
		return $this->jobtitle;
	}

}
\Fox\Dao::infos(Experience::className(),new \Fox\Object\Info('ressource_experiences',
\Fox\Databases::DB_APP))
->addAutoIncrementField('expid');
